<?php

/**
 *
 * @author Kinmeic
 *        
 */
class Session {
	// The state of the session
	private $state = FALSE;
	
	// THE only instance of the class
	private static $instance;
	
	/**
	 */
	function __construct() {}
	
	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
	
	/**
	 * 取得唯一实例
	 * @return Session
	 */
	public static function getInstance() {
		if ( !isset(self::$instance))
		{
			self::$instance = new self;
		}
		
		self::$instance->startSession();

		return self::$instance;
	}
	
	/**
	 * 开始线程
	 * @return boolean
	 */
	public function startSession(){
		if ($this->state == FALSE)
		{
			$this->state = session_start();
		}
		
		return $this->state;
	}
	
	/**
	 * @param string $name
	 * @param mixed $default
	 * @return mixed
	 */
	function __get($name) {
		if (isset($_SESSION[$name])) {
			return $_SESSION[$name];
		}
	}
	
	/**
	 * @param string $name
	 * @param mixed $value
	 */
	function __set($name, $value) {
		$_SESSION[$name] = $value;
	}
	
	/**
	 * @param string $name
	 */
	function __isset($name) {
		return isset($_SESSION[$name]);
	}
	
	/**
	 * @param string $name
	 */
	function __unset($name) {
		unset($_SESSION[$name]);
	}
	
	/**
	 * Destroys the current session.
	 */
	function destroy() {
		if (session_destroy()) {
			unset( $_SESSION );
		}
	}
}

?>